package app.android.alampang.loader;

import android.content.Context;
import android.content.res.AssetManager;

import org.w3c.dom.Element;

import java.io.InputStream;
import java.util.ArrayList;
import app.android.alampang.model.Place;
import app.android.alampang.model.PlaceGroup;
import app.android.alampang.tool.XMLReader;

/**
 * Created by Supanut on 1/20/2015.
 */
public class PlaceInfoLoader
{
    private static String xml;
    private static XMLReader reader;

    private static ArrayList<Place> places = new ArrayList<>();
    private static ArrayList<PlaceGroup> groups = new ArrayList<>();

    public static void load(String path, Context context) throws Exception
    {
        AssetManager manager = context.getAssets();
        //InputStream file = manager.open("info/place_info.json");
        InputStream file = manager.open(path);
        byte[] data = new byte[file.available()];
        file.read(data);
        file.close();

        xml = new String(data, "UTF-8");
        reader = new XMLReader();
        reader.read(xml);

        parseAllPlaces(context);
        parseAllPlaceGroups(context);
    }

    private static void parseAllPlaces(Context context)
    {
        reader.moveTo("Place");
        for (int i = 0; i < reader.getNodeLength(); i++)
        {
            reader.setNodeIndex(i);

            Place place = new Place();
            place.setName(reader.getContent("Name"));
            place.setAlternativeName(reader.getContent("AltName"));
            int drawable = context.getResources()
                    .getIdentifier(reader.getContent("ImageName"), "drawable", context.getPackageName());
            place.setImageDrawable(drawable);
            place.setHilightColor(reader.getContent("HLColor"));
            place.setLatitude(reader.getContent("Latitude"));
            place.setLongitude(reader.getContent("Longitude"));
            place.setContent(reader.getContent("Content"));

            places.add(place);
        }
    }

    private static void parseAllPlaceGroups(Context context)
    {
        Element element = reader.moveTo("Group");
        int groupCount = reader.getNodeLength();

        for (int i = 0; i < groupCount; i++)
        {
            reader.setNodeIndex(i);

            PlaceGroup group = new PlaceGroup();
            group.setGroupName(reader.getContent("Name"));
            int drawable = context.getResources()
                    .getIdentifier(reader.getContent("ImageName"), "drawable", context.getPackageName());
            group.setImageDrawable(drawable);

            //ArrayList<Place> places = new ArrayList<>();

            reader.moveTo("Place", element);
            int placeCount = reader.getNodeLength();

            for (int j = 0; j < placeCount; j++)
            {
                reader.setNodeIndex(j);

                Place place = new Place();

                place.setName(reader.getContent("Name"));
                place.setAlternativeName(reader.getContent("AltName"));
                int pDrawable = context.getResources()
                        .getIdentifier(reader.getContent("ImageName"), "drawable", context.getPackageName());
                place.setImageDrawable(pDrawable);
                place.setHilightColor(reader.getContent("HLColor"));
                place.setLatitude(reader.getContent("Latitude"));
                place.setLongitude(reader.getContent("Longitude"));
                place.setContent(reader.getContent("Content"));

                group.addPlace(place);
            }

            groups.add(group);
            reader.moveTo("Group");
        }
    }

    public static ArrayList<Place> getAllPlaces()
    {
        return places;
    }

    public static ArrayList<PlaceGroup> getAllPlaceGroups()
    {
        return groups;
    }

    public static Place getPlaceByName(String name)
    {
        Place place = null;
        for (Place p : places)
        {
            if (p.getName().equals(name))
                return p;
        }
        return place;
    }




}